using System;
using System.Security.Claims;
using System.Threading;
using ZQCY.Watchmen.Extensions.Extensions;
using Microsoft.AspNetCore.Http;
using HBKY.Shop.Common.RequestAccessor;
using HBKY.Shop.Extensions.Extensions;

namespace HBKY.Shop.Extensions.AuthHelper.RequestAccessor
{
    public class CurrentUser : ICurrentUser
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Guid _scopeId;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="httpContextAccessor">HttpContext访问器</param>
        public CurrentUser(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _scopeId = Guid.NewGuid();
        }

        private ClaimsPrincipal Accessor =>
            _httpContextAccessor.HttpContext?.User ?? Thread.CurrentPrincipal as ClaimsPrincipal;

        public Guid ScopeId => _scopeId;
        public int LoginId => Accessor?.GetLoginId() ?? -1;
        public int UserId => Accessor?.GetUserId() ?? -1;
        public string UserName => Accessor?.GetName() ?? string.Empty;
        public string DisplayName => Accessor?.GetDisplayName() ?? string.Empty;
        public string Avatar => Accessor?.GetAvatar() ?? string.Empty;

        /// <summary>
        /// 角色
        /// </summary>
        public int[] RoleIds => Accessor?.GetRoleIds();
    }
}